import 'dart:ui';

import 'package:flame/components.dart';
import 'package:flame/effects.dart';
import 'package:flame/sprite.dart';

class TouchIndicator extends SpriteAnimationComponent {
  late Image image;

  TouchIndicator({
    required Vector2 position,
    required this.image,
  }) : super(
          size: Vector2(30, 30),
          anchor: Anchor.center,
          position: position,
        );

  @override
  Future<void> onLoad() async {
    SpriteSheet sheet =
        SpriteSheet.fromColumnsAndRows(image: image, columns: 5, rows: 3);
    List<Sprite> sprites = List.generate(11, sheet.getSpriteById);
    removeOnFinish = true;
    animation =
        SpriteAnimation.spriteList(sprites, stepTime: 1 / 11, loop: false);
  }
}

class Bullet extends SpriteComponent {
  double speed = 200;
  final double maxRange;
  double length = 0;
  Bullet({required Sprite sprite, required this.maxRange})
      : super(sprite: sprite);

  @override
  void update(double dt) {
    super.update(dt);
    Vector2 ds = Vector2(1, 0) * speed * dt;
    length += ds.length;
    position.add(ds);
    if(length > maxRange) {
      length = 0;
      removeFromParent();
    }
  }
}
